%
% Vasyl Mykhalchuk
%
function [ sfMeanGlobal, meanCurv, adjVertList, obj_ref, strain, bending ] = getScalarFields( type, numFrames )
% PRE-LOADING
switch type
    case dataType.kHorse
        name = 'horse';
    case dataType.kCamel
        name = 'camel';
    case dataType.kFace
        name = 'Take0006';
    case dataType.kFemale
        name = 'female4';
    case dataType.kCloth
        name = 'SimpleCloth';
end % SWITCH

resDir = ['../../res/' name '/'];
dataFile = [resDir name '.mat'];
refFile = [resDir name 'Ref.mat'];

% LOAD DATA
data = load( dataFile );
refDataFile = load( refFile );
[numberOfFrames, nV] = size( data.frames );
if ( numFrames ~= 0 )
    numberOfFrames = numFrames;
end % IF
nV = nV / 3;
nF = size( data.topology, 1 );
obj_ref.vertices = refDataFile.ref;
obj_ref.vertices = toMatrix( data.frames( 1, : ) );
obj_ref.faces = data.topology;
obj_ref.nF = nF;
obj_ref.nV = nV;
% END LOAD DATA

obj_ref.boundingSize = bbox( obj_ref.vertices );

% COMPUTE REFERENCE CURVATURE
refCurv = meshCurvature( data.topology, obj_ref.vertices );
k(:, 1) = ( refCurv.k1 .* refCurv.k2 ) ;

% DEFAULT INITIALIZATION
sfMeanGlobal = zeros( obj_ref.nV, 1 );
gaussCurvature = zeros( obj_ref.nV, numberOfFrames );
deltaCurvature = zeros( obj_ref.nV, numberOfFrames );
strain = zeros( obj_ref.nV, numberOfFrames );
% LOAD ADJ LIST
adjDataFile = load( [resDir name 'Adj.mat'] );
adjList = adjDataFile.adjList;
adjVertList = adjDataFile.adjVertList;
% END LOAD DATA

% PROCESSING
stepCount = 0;
for targetId = 1:numberOfFrames
    vertices = toMatrix( data.frames( targetId, : ) );
    obj_target.vertices = vertices;
    obj_target.faces = data.topology;
    sf = nextSF( obj_target, obj_ref, adjList, adjVertList );    
    sfMeanGlobal = sfMeanGlobal + sf;
    stepCount = stepCount + 1;
        
    strain(:, targetId) = sf;
    
    curvature = meshCurvature(obj_target.faces, obj_target.vertices);
    gaussCurvature(:, targetId) = ( curvature.k1 .* curvature.k2 );    
end % FOR
% COMPUTE AVERAGE STRAIN
sfMeanGlobal = sfMeanGlobal / stepCount;
%{
maxMean = max( abs( sfMeanGlobal ) );
if ( maxMean ~= 0 )
    sfMeanGlobal = sfMeanGlobal / maxMean;
end % if
%}

% COMPUTE DELTA CURVATURE
kRef = k(:,1);
for frameId = 1:numberOfFrames    
    deltaCurvature(:, frameId) = gaussCurvature(:, frameId) - kRef;
end % FOR

% COMPUTE MEAN CURVATURE
meanCurv = mean( deltaCurvature, 2);
maxCurv = max( abs(meanCurv) );
if( maxCurv ~= 0 )
    meanCurv = meanCurv / maxCurv;
end % if

bending = deltaCurvature;

%{
for i = 1:nV
    if ( abs( meanCurv(i, 1) ) < 0.1 )
        meanCurv(i, 1) = 0.0;
    end % if
end % for
%}


end % function

